'''
公路(Road):属性、公路名称、公路长度
车(Car):属性、车名、时速
        方法：1.求车名在那条公路上以多少的时速行驶多长时间
                get_time(self,road)
            2.初始化车属性信息__init__方法
            3.打印对象显示车的属性信息
'''
import random


class Road:
    def __init__(self,name,length):
        self.name=name
        self.length=length

class Car:
    def __init__(self,brand,speed):
        self.brand=brand
        self.speed=speed

    def get_time(self,road): # self:Car对象，road:Road对象
        ran_time = random.randint(1, 10)
        msg = '{}品牌的车在{}公路上以{}的速度行驶了{}小时'.format(self.brand,road.name,self.speed,ran_time)
        print(msg)

    def __str__(self):
        return '品牌：{}，时速：{}'.format(self.brand,self.speed)

r = Road('G50',1000)
print(r.name)
r.name = 'G60'
c = Car('奔驰',120)

print(c)
c.get_time(r)
print('--------------------------')
'''
1.has a
     一个类使用了另外一种自定义的类型
     student使用了computer book
2.类型
     系统类型：
         str int float bool 
         list tuple dict set
     自定义类型：
         算是自定义的类，都可以将其当成一种类型
         s = Student()
         s是Student类型的对象
'''
class Computer:
    def __init__(self,brand,type,color):
        self.brand = brand
        self.type = type
        self.color = color

    def online(self):
        print('正在使用电脑上网...')

    def __str__(self):
        return self.brand + '--' + self.type + '--' + self.color


class Book:
    def __init__(self,bname,author,number):
        self.bname = bname
        self.author = author
        self.number = number

    def __str__(self):
        return self.bname + '--' + self.author + '--' + str(self.number)


class student:# has a
    def __init__(self,name,computer,book):
        self.name = name
        self.computer = computer
        self.books = []
        self.books.append(book)

    def borrow_book(self,book):
        for book1 in self.books:
            if book1.bname == book.bname:
                print('已经借阅过这本书了')
                break
        else:
            self.books.append(book)
            print('借阅成功')

    def __str__(self):
        return self.name + '--' + str(self.computer) + '--' + str(self.books)

computer = Computer('联想','台式机','黑色')
book = Book('红楼梦','曹雪芹',1)
stu = student('小明',computer,book)
print(stu)

stu.borrow_book()
book1 = Book('西游记','吴承恩',2)
stu.borrow_book(book1)
print('--------------------------')